package com.lemon.bean.stageBean;

import com.lemon.beanManager.BeanContainer;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static com.lemon.beanManager.BeanContainer.beanFactory;

public class LoadSingleRename {
    //单选重命名窗口

    private Pane singleRenameWindow;
    private Stage stage = new Stage();
    private Text waitRenameText;
    private String waitRenameFilename;

    public Label title;
    public TextField renameTextField;
    public Button confirm;
    public Button cancel;

    //data
    private String dupDestPath="";
    private String dupNewFileName = "";

    public void loadView(){
        try {
            singleRenameWindow = FXMLLoader.load(getClass().getResource("Rename.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(singleRenameWindow,240,150);
        stage.setScene(scene);
        stage.setWidth(240);
        stage.setHeight(150);
        stage.toFront();
        stage.initStyle(StageStyle.UNDECORATED);

        renameTextField = (TextField) beanFactory.getBean("renameText");

    }
    public void initialFunction(){
        Button renameConfirm = (Button) beanFactory.getBean("renameConfirm");
        renameConfirm.setOnAction(e->{
            if(waitRenameText!=null){
                confirm();
            }else{
                String text = renameTextField.getText();
                if("".equals(text)||text==null){
                    text=dupDestPath+"副本lemon_non_dup";
                }
                dupNewFileName = text;
            }
            close();
        });

        Button renameCancel = (Button) beanFactory.getBean("renameCancel");
        renameCancel.setOnMouseClicked(e->{
            close();
        });

        URL resource = this.getClass().getResource("css/singleRename.css");
        addStyle(resource.toExternalForm());

    }

    private void confirm() {
        String oldText = waitRenameText.getText();
        String fileType = oldText.substring(oldText.lastIndexOf("."));
        //text名字更改
        String newName = renameTextField.getText() + fileType;

        waitRenameText.setText(newName);

        String filename = this.waitRenameFilename;
        File oldFile = new File(filename);
        //todo /和\的兼容处理
        String filePath = filename.substring(0, filename.lastIndexOf("/"));

        File newFile = new File(filePath +"/"+ newName);
        boolean b = oldFile.renameTo(newFile);

        ImageView imageView = (ImageView) waitRenameText.getParent().lookup("ImageView");
        imageView.setImage(new Image("file:"+newFile.getAbsolutePath()));

        if(!b){
            System.gc();
            System.out.println("普通改名失败");
            boolean b1 = oldFile.renameTo(newFile);
            if(!b1)
                System.out.println("强制改名失败");
        }
    }

    public void show(){
        stage.show();
    }
    public void close(){
        beanFactory.remove("renameConfirm");
        beanFactory.remove("renameCancel");
        beanFactory.remove("renameText");
        beanFactory.remove("title");
        beanFactory.remove("renameAn");
        stage.close();
    }

    public Label getTitle(){
        return  (Label) beanFactory.getBean("title");
    }

    public void setRenameImage(Text text,String filename) {
        waitRenameText=text;
        waitRenameFilename=filename;
    }
    public void addStyle(String cssPath){
        singleRenameWindow.getStylesheets().add(cssPath);
    }
    public void setDupFile(String  destPath) {
        this.dupDestPath = destPath;
    }
    public String getDupNewFileName() {
        return null;
    }
}
